import Vue from 'vue';
import Vuex from 'vuex';

import dispath from './dispath';

import { queryCameraUrl } from '@/api/item/monitor/video.js';
import { getInWellPersonInfo } from '@/api/item/dispatch/index';

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    // 视频信息
    videoInfo: {
      isVideo: false,
      name: '',
      status: '',
      videoLink: ''
    },
    coalFlowVideoTitle: null, // 智能煤流视频选中
    webToServer: {},
    webToScene: {},
    // 两侧面板状态
    boxStatus: true,
    // 井下人员
    inwellNum: 0,
    // 井下超时人员
    overTimeNum: 0,
    sceneLoaded: null,
    // cocos
    WebEngineBridge: null,
    // 人员
    personnelStatus: null,
    // 告警
    alarmStatus: null,
    // 矿压
    minepressure: null,
    // 视频
    ePointMonitor: {
      activeTitle: '',
      current: ''
    }
  },
  mutations: {
    setWebToServer(state, value) {
      state.webToServer = value;
    },
    setEPointMonitor(state, payload) {
      const { name, value } = payload;
      state.ePointMonitor[name] = value;
    },
    setData(state, payload) {
      const { name, value } = payload;
      state[name] = value;
    }
  },
  actions: {

    // web端发送消息至引擎
    async postToEngine({ state }, params) {
      let record = null;
      const { WebEngineBridge } = state;
      const { sceneName, data } = params;
      if (data !== undefined) {
        record = data;
      }
      console.log('当前事件名称---', sceneName);
      console.log('当前事件数据---', record);
      if (WebEngineBridge) {
        const res = await WebEngineBridge.postToEngine(sceneName, record).catch(e => console.log(e));
        console.log('WebEngineBridge-postToEngine', res);
        return res;
      }
    },

    // 监听 PERSONNEL 事件
    listeningEvents({ state }, params) {
      const { WebEngineBridge } = state;
      const { fn, _this } = params;
      if (WebEngineBridge) {
        WebEngineBridge.onEngineMessage(
          'POINT',
          fn,
          _this
        );
      }
    },

    // 取消监听 PERSONNEL 事件
    closeListeningEvents({ state }, params) {
      const { WebEngineBridge } = state;
      const { fn, _this } = params;
      if (WebEngineBridge) {
        WebEngineBridge.offEngineMessage(
          'POINT',
          fn,
          _this
        );
      }
    },

    // 场景加载
    sceneLoaded({ state, commit }) {
      const { WebEngineBridge } = state;
      console.log('before');
      if (WebEngineBridge) {
        console.log('WebEngineBridge---before');
        WebEngineBridge.onceEngineMessage(
          'SceneLoaded',
          (msg) => {
            commit('setData', { name: 'sceneLoaded', value: msg.data });
            console.log(msg.data);

            msg.resolve({ response: 'successs' }); // 必须执行 msg.resolve()
          }
        );
      }
    },

    // 公共请求视频地址流
    async queryCameraUrl({ commit }, params) {
      /*
        不同协议返回的视频地址流格式不同。
        默认是 hls
          Rtsp协议时传入参数示例：
          {
              "cameraIndexCode": "",
              "protocol": "rtsp",
              "expand": "streamform=rtp"
          }
          Rtmp协议时传入参数示例：
          {
              "cameraIndexCode": "",
              "protocol": "rtmp",
              "expand": "transcode=0"

          }
          hls协议时传入参数示例：
          {
              "cameraIndexCode": "",
              "protocol": "hls",
              "expand": "transcode=1&videotype=h264"
          }
      */
      // status true/false 表示是否在 安全监控-视频监控模块下
      const { videoInfo } = params;

      const defaultParams = {
        'protocol': 'hls',
        'expand': 'transcode=1&videotype=h264'
      };
      defaultParams['cameraIndexCode'] = videoInfo?.cameraIndexCode;

      const res = await queryCameraUrl(defaultParams);
      commit('setData', { name: 'videoInfo', value: {
        isVideo: true,
        name: videoInfo?.cameraName,
        status: videoInfo?.online === 1 ? '正常' : videoInfo?.online === 0 ? '异常' : '',
        videoLink: res.data?.data?.url
      }});
    },

    // 获取井下人数信息(统一调度页面和 header 数据共享)
    async getInWellPersonInfo({ commit }) {
      const res = await getInWellPersonInfo();
      if (res.data.code === 200) {
        const inwellNum = res.data.data.inwellNum;
        const overTimeNum = res.data.data.overTimeNum;

        commit('setData', { name: 'inwellNum', value: inwellNum });
        commit('setData', { name: 'overTimeNum', value: overTimeNum });
      }
    }
  },
  modules: {
    dispath
  }
});
